package com.wande.dataplatform.filecollection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.core.validate.AddGroup;
import com.wande.common.core.validate.EditGroup;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.filecollection.domain.bo.MappingRuleBo;
import com.wande.dataplatform.filecollection.domain.dto.FieldInfo;
import com.wande.dataplatform.filecollection.domain.dto.ValidationResult;
import com.wande.dataplatform.filecollection.domain.vo.MappingRuleVo;
import com.wande.dataplatform.filecollection.service.IFileMappingService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件映射控制器
 *
 * @author wande
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/file-collection/mapping")
public class FileMappingController extends BaseController {

    private final IFileMappingService fileMappingService;

    /**
     * 创建映射规则
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:add")
    @Log(title = "文件映射", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Validated(AddGroup.class) @RequestBody MappingRuleBo bo) {
        return R.ok(fileMappingService.createMappingRule(bo));
    }

    /**
     * 更新映射规则
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:edit")
    @Log(title = "文件映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MappingRuleBo bo) {
        fileMappingService.updateMappingRule(bo);
        return R.ok();
    }

    /**
     * 删除映射规则
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:remove")
    @Log(title = "文件映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        fileMappingService.deleteMappingRule(id);
        return R.ok();
    }

    /**
     * 获取映射规则详情
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @GetMapping("/{id}")
    public R<MappingRuleVo> getInfo(@PathVariable Long id) {
        return R.ok(fileMappingService.getMappingRule(id));
    }

    /**
     * 查询映射规则列表
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:list")
    @GetMapping("/list")
    public TableDataInfo<MappingRuleVo> list(MappingRuleBo bo, PageQuery pageQuery) {
        return fileMappingService.listMappingRules(bo, pageQuery);
    }

    /**
     * 检测文件字段
     * 上传文件并自动识别字段结构
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:detect")
    @PostMapping("/detect-fields")
    public R<List<FieldInfo>> detectFields(
        @RequestParam("file") MultipartFile file,
        @RequestParam("fileType") String fileType) {
        return R.ok(fileMappingService.detectFileFields(file, fileType));
    }

    /**
     * 查询目标表列表
     * 从Doris数据库查询所有表
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @GetMapping("/target-tables")
    public R<List<String>> listTargetTables(@RequestParam("database") String database) {
        return R.ok(fileMappingService.listTargetTables(database));
    }

    /**
     * 获取表字段信息
     * 从Doris数据库查询表的字段结构
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @GetMapping("/table-fields")
    public R<List<FieldInfo>> getTableFields(
        @RequestParam("database") String database,
        @RequestParam("table") String table) {
        return R.ok(fileMappingService.getTableFields(database, table));
    }

    /**
     * 验证映射规则
     * 验证映射配置的正确性
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @PostMapping("/validate")
    public R<ValidationResult> validateMapping(@RequestBody MappingRuleBo bo) {
        return R.ok(fileMappingService.validateMapping(bo));
    }

    /**
     * 测试校验规则
     * 使用示例数据测试校验规则配置
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @PostMapping("/test-validation")
    public R<ValidationResult> testValidation(@RequestBody MappingRuleBo bo) {
        return R.ok(fileMappingService.testValidationRules(bo));
    }

    /**
     * 获取可用的校验规则类型
     */
    @SaCheckPermission("dataplatform:file-collection:mapping:query")
    @GetMapping("/validation-types")
    public R<List<String>> getValidationTypes() {
        return R.ok(fileMappingService.getAvailableValidationTypes());
    }
}
